package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.Page;

import admin.forms.PageForm;
import admin.models.PageModel;



public class PageServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		String title = getTitle(id);
		
		if (title == null) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		PageModel pageModel = PageModel.getInstance();
		Page page = pageModel.getPage(id);
		
		if (page == null) {
			page = new Page();
			page.setId(id);
		}
		
		request.setAttribute("page", page);
		request.setAttribute("check", check);
		request.setAttribute("title", title);
		try {
			request.getRequestDispatcher("/admin/pages/static_page.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		TreeMap check = new TreeMap();
		
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		String title = getTitle(id);
		
		if (title == null) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		PageModel pageModel = PageModel.getInstance();
		Page page = pageModel.getPage(id);
		
		if (page == null) {
			page = new Page();
			page.setId(id);
		}
		
		PageForm pageForm = new PageForm();
		TreeMap form = pageForm.checkValid(request, page);
		check = (TreeMap) form.get("checkValid");
		page = (Page) form.get("page");
		
		if (check.size() == 0) {
			page.setUpdate(new Date());
			pageModel.insert(page);
			session.setAttribute("success", "Page " + title + " has been updated success.");
		}
		
		request.setAttribute("page", page);
		request.setAttribute("check", check);
		request.setAttribute("title", title);
		try {
			request.getRequestDispatcher("/admin/pages/static_page.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	private String getTitle(String id) {
		String title = null;
		
		if (id.equalsIgnoreCase("about-us")) {
			title = "About Us";
		}
		
		if (id.equalsIgnoreCase("privacy-policy")) {
			title = "Privacy Policy";
		}
		
		if (id.equalsIgnoreCase("terms-conditions")) {
			title = "Terms & Conditions";
		}
		
		return title;
	}
}
